/**   

* @Title: CustomerServiceController.java 
* @Package com.kerrykidz.filemanagement.controller 
* @Description: 
* Copyright: Copyright (c) 2014 
* Company:wuwh team by iss
* @author: ScreenJxp   
* @date: 2014年9月5日 下午4:24:08 
* @version: V1.0
*
*/
package com.kerrykidz.filemanagement.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.filemanagement.dao.model.CarCareFormEntity;
import com.kerrykidz.filemanagement.dao.model.VipUserEntity;
import com.kerrykidz.filemanagement.service.ICustomerService;
import com.kerrykidz.filemanagement.service.IVipService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.dao.model.SecUsrEntity;
import com.kerrykidz.system.service.IUserService;
import com.kerrykidz.system.util.ConstantUtil;
import com.kerrykidz.system.util.DataSourceUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.util.ConstantUtil.WorkflowKey;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.kerrykidz.workflow.vo.WorkflowParamVo;
import com.rdp.framework.core.controller.SpringSuperController;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.lang.StringUtil;
import com.rdp.util.time.DateFormatUtil;

@Controller
@RequestMapping("filemanagement/customer")
public class CustomerServiceController extends SpringSuperController {
	@Autowired
	private ICommonService commonService;
	
	@Autowired
	private ICustomerService customerService;
	
	@Autowired 
	private IWorkflowService workflowService;
	//会员管理
	@Autowired
	private  IVipService vipService;

	//用户管理
	@Autowired
	private IUserService  userService;
	
	@Autowired
	DataSourceUtil das;
	
	/**
	 * 跳转客户服务界面
	 * 
	 * @return
	 * @throws RDPException 
	 */
	@RequestMapping("/serviceList")
	public ModelAndView serviceList() throws RDPException {
		ModelAndView mav= new ModelAndView("file/file_service");
		GblSecBranEntity bran=new GblSecBranEntity();
		String branId=UserUtil.getUserFromSession().getBranId();
		if(Constants.HEAD_BRAN_ID.equals(branId)){
			bran.setExcluedId(Constants.HEAD_BRAN_ID);
			mav.addObject("branList", commonService.getBranList(bran));
		}else{
			bran.setPkBranId(branId);
			mav.addObject("branList", commonService.getBranList(bran));
		}
		return mav;
	}
	@RequestMapping("/findServiceList")
	@ResponseBody
	public CarCareFormEntity findServiceList(CarCareFormEntity entity) throws RDPException{
			entity.setIspaging(true);
			entity.setUserId(UserUtil.getUserFromSession().getUserId());
			if(entity.getCreateStartTime()!=null&&!"".equals(entity.getCreateStartTime())){
				entity.setCreateStartTime(entity.getCreateStartTime().concat(" 00:00:00"));
			}
			if(entity.getCreateEndTime()!=null&&!"".equals(entity.getCreateEndTime())){
				entity.setCreateEndTime(entity.getCreateEndTime().concat(" 23:59:59"));
			}
			entity.setResults(customerService.findServiceList(entity));
			return entity;
	}
	/**
	 *跳转到投诉页面
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月5日
	 */
	@RequestMapping("/complainPage")
	public ModelAndView complainPage() throws RDPException {
		UserInfo user=UserUtil.getUserFromSession();
		String nowDate=DateFormatUtil.convertDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss");
		ModelAndView mav= new ModelAndView("file/file_complaint");
		mav.addObject("createTime",nowDate);
		mav.addObject("userName",user.getName());
		mav.addObject("userId", user.getUserId());
		mav.addObject("fkBranId", user.getBranId());
		return mav;
	}
	/**
	 * 申请服务页面
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("/applyServicePage")
	public ModelAndView  applyServicePage() throws RDPException {
		String nowDate=DateFormatUtil.convertDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss");
		UserInfo user=UserUtil.getUserFromSession();
		ModelAndView mav= new ModelAndView("file/file_applyservice");
		List<GblSecBranEntity> branList=null;
		GblSecBranEntity bran=new GblSecBranEntity();
		if(Constants.HEAD_OFFICE_CODE.equals(user.getAbbrCode())){
			bran.setPkBranId(user.getBranId());
			branList=commonService.getBranList(bran);
		}else{
			ArrayList<String> list=new ArrayList<String>();
			list.add(Constants.HEAD_BRAN_ID);
			list.add(user.getBranId());
			bran.setIncludeId(list);
			branList=commonService.getBranList(bran);
		}
		mav.addObject("branId", user.getBranId());
		mav.addObject("branList", branList);
		mav.addObject("createTime", nowDate);
		mav.addObject("userName", user.getName());
		return mav;
	}
	/**
	 * 保存服务请求
	 * @param entity
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月5日
	 */
	@RequestMapping("/saveApplyCustomer")
	@ResponseBody
	public String saveApplyCustomer(CarCareFormEntity entity) throws RDPException{
			UserInfo info=UserUtil.getUserFromSession(); 
			String pkCareformId=PrimaryKeyGenerator.getPrimaryKey(BusinessType.KF, info.getAbbrCode());
		    entity.setPkCareformId(pkCareformId);
		    entity.setFkBranId(info.getBranId());
		    entity.setApplyBy(info.getUserId());
		    entity.setModiBy(entity.getHandleId());
		    entity.setFkMemId("".equals(entity.getFkMemId())?null:entity.getFkMemId());
		    //申请服务，流程
		    if(Constants.CUSTOMER_SERVICE_APPLY.equals(entity.getCareType())){
		    		if("2".equals(entity.getStatus())){
		    			  String fkFlowinstId=workflowService.startWorkflow(ConstantUtil.getWorkflowKey(WorkflowKey.server), pkCareformId, info);
		    			  entity.setFkFlowinstId(fkFlowinstId);
		    			  entity.setModiBy(entity.getHandleId());
		    			  customerService.saveCustomerService(entity);
		    			 if(StringUtil.isNotBlank(info.getBranId())){
								//如果是门店的话就进行数据上传
								if(info.getBranId().indexOf("ZB") == -1){
			    				    CarCareFormEntity car=  customerService.findServiceById(entity.getPkCareformId(), "");
									das.executeServer(car, "insert");
								}
							}
		    			  //指定流程处理人
		    			  WorkflowParamVo workflowparam=new WorkflowParamVo();
		    			  workflowparam.setProcessInstanceId(fkFlowinstId);
		    			  workflowparam.setClaimUserId(entity.getHandleId());//总监
		    			  workflowService.claimTask(workflowparam);
		    		}
		    		else{
						customerService.saveCustomerService(entity);
		    		}
		    }else{
		    	if("2".equals(entity.getStatus())){
		    		entity.setStatus("3");
		    	}
				customerService.saveCustomerService(entity);
		    }
		return Constants.SUCCESS;
	}
	@RequestMapping("/updateAppService")
	public ModelAndView updateAppService(@RequestParam("pkCareformId")String   pkCareformId) throws RDPException{
		ModelAndView mav=new ModelAndView("file/file_UpdateApplyService");
		UserInfo user=UserUtil.getUserFromSession(); 
		List<GblSecBranEntity> branList=null;
		GblSecBranEntity bran=new GblSecBranEntity();
		if(Constants.HEAD_OFFICE_CODE.equals(user.getAbbrCode())){
			bran.setPkBranId(user.getBranId());
			branList=commonService.getBranList(bran);
		}else{
			ArrayList<String> list=new ArrayList<String>();
			list.add(Constants.HEAD_BRAN_ID);
			list.add(user.getBranId());
			bran.setIncludeId(list);
			branList=commonService.getBranList(bran);
		}
		mav.addObject("branList", branList);
		mav.addObject("branId", user.getBranId());
		mav.addObject("baseEntity",customerService.findServiceById(pkCareformId, ""));
		mav.addObject("handEntity", customerService.findForApply(pkCareformId));
		return mav;
	}
	/**
	 * 修改客户服务
	 * @param entity
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月18日
	 */
	@RequestMapping("/updateCustomerService")
	@ResponseBody
	public  String   updateCustomerService(CarCareFormEntity entity) throws RDPException{
		UserInfo info=UserUtil.getUserFromSession(); 
		entity.setModiBy(entity.getHandleId());
		if(Constants.CUSTOMER_SERVICE_APPLY.equals(entity.getCareType())){
			     entity.setModiBy(entity.getHandleId());
		    	 if("2".equals(entity.getStatus())){
		    		  String fkFlowinstId=workflowService.startWorkflow(ConstantUtil.getWorkflowKey(WorkflowKey.server), entity.getPkCareformId(), info);
	    			  entity.setFkFlowinstId(fkFlowinstId);
	    			  customerService.updateCustomerService(entity);
	    			  if(StringUtil.isNotBlank(info.getBranId())){
							//如果是门店的话就进行数据上传
							if(info.getBranId().indexOf("ZB") == -1){
		    				    CarCareFormEntity car=  customerService.findServiceById(entity.getPkCareformId(), "");
								das.executeServer(car, "insert");
							}
						}
	    			//指定流程处理人
	    			  WorkflowParamVo workflowparam=new WorkflowParamVo();
	    			  workflowparam.setProcessInstanceId(fkFlowinstId);
	    			  workflowparam.setClaimUserId(entity.getHandleId());
	    			  workflowService.claimTask(workflowparam);
		    	 }else{
		    		 customerService.updateCustomerService(entity);
		    	 }
		 }else{
			    customerService.updateCustomerService(entity);
		 }
		return Constants.SUCCESS;
	}
	@RequestMapping("/deleteCustomerService")
	@ResponseBody
	public  String deleteCustomerService(@RequestParam("pkCareformId")String pkCareformId) throws RDPException{
			customerService.deleteCustomerService(pkCareformId);
			return Constants.SUCCESS;
	}
	@RequestMapping("/getVipUserInfo")
	@ResponseBody
	public  VipUserEntity getVipUserInfo(VipUserEntity model){
		Map<String, Object>  params=new HashMap<String, Object>();
		//开启分页
		params.put(Constants.MEMBER_QUERY_TYPE,"2");
		String condition=model.getNameOrTel();
		params.put(Constants.MEMBER_CONDITION, condition);
		model.setParams(params);
	    List<VipUserEntity> list = null;
		// 判断那种类型
		if (null != condition && !"".equals(condition) && StringUtils.isNumeric(condition) == true) {
			// 号码查询
			params.put(Constants.MEMBER_TELNUM, condition);
			model.setParams(params);
			list = vipService.findMemberByTelOrEmail(model);
		} else if (null != condition && !"".equals(condition)
				&& (condition.indexOf("@") != -1 || condition.indexOf(".") != -1)) {
			// 邮箱查询
			params.put(Constants.MEMBER_EMAIL, condition);
			model.setParams(params);
			list = vipService.findMemberByTelOrEmail(model);
		} else {
			// 姓名查询
			model.setParams(params);
			list = vipService.listVipUser(model);
		}
	    model.setResults(list);
	    return model;
	}
	@RequestMapping("/updateComplaintPage")
	@ResponseBody
	public ModelAndView updateComplaintPage(@RequestParam("pkCareformId")String pkCareformId) throws RDPException{
		UserInfo info=UserUtil.getUserFromSession(); 
		ModelAndView mav= new ModelAndView("file/file_updateComplaint");
		mav.addObject("entity", customerService.findServiceById(pkCareformId,info.getUserId()));
		return mav;
	}
	@RequestMapping("/updateServicePage")
	@ResponseBody
	public ModelAndView updateServicePage(@RequestParam("pkCareformId")String pkCareformId) throws RDPException{
		UserInfo info=UserUtil.getUserFromSession(); 
		ModelAndView mav= new ModelAndView("file/file_updatomplaint");
		mav.addObject("entity", customerService.findServiceById(pkCareformId,info.getUserId()));
		return mav;
	}
	/**
	 * 仅仅查看投诉信息
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月15日
	 */
	@RequestMapping("/readComplainInfo")
	@ResponseBody
	public  ModelAndView  readComplainInfo(@RequestParam("pkCareformId")String pkCareformId) throws RDPException{
		UserInfo info=UserUtil.getUserFromSession(); 
		ModelAndView mav= new ModelAndView("file/file_NotEditComplaint");
		mav.addObject("entity", customerService.findServiceById(pkCareformId,info.getUserId()));
		return mav;
	}
	@RequestMapping("/handleAppServier")
	@ResponseBody
	public  String   handleAppServier(WorkflowParamVo vo,CarCareFormEntity car) throws RDPException{
		UserInfo userInfo=UserUtil.getUserFromSession();
		vo.setProcessInstanceId(car.getFkFlowinstId());
		//当前用户审批操作
		String status=car.getStatus();
		if("3".equals(status)){
				//是否继续上报。交给下一级处理
			    if(!"".equals(vo.getClaimUserId())){
			    	  vo.setHandleParam("isNext");
			    	  vo.setHandleParamValue(true);
			    	   vo.setHandleAdviceDes("同意");
			    	   vo.setUserInfo(userInfo);
			    	   ProcessInstance p =  workflowService.complete(vo);
			    	   if(null != p){
			    		   workflowService.claimTask(vo);
			    	   }
			    	  
			    }else{//当前用户已经允许此操作
			    		//完成任务
			    	vo.setUserInfo(userInfo);
			    	vo.setHandleAdviceDes("同意");
			    	vo.setHandleParam("isNext");
			    	vo.setHandleParamValue(false);
			    	ProcessInstance instance=workflowService.complete(vo);
			    	if(instance==null){
			    		//执行成功
			    		car.setStatus("3");
				    	customerService.updateCustomerService(car);
			    	}
			    }
		}else if("4".equals(status)){
				//是否继续上报
		    if(!"".equals(vo.getClaimUserId())){
		    	  vo.setHandleParam("isNext");
		    	  vo.setHandleParamValue(true);
		    	   vo.setHandleAdviceDes("不同意");
		    	   vo.setUserInfo(userInfo);
		    	   ProcessInstance p =  workflowService.complete(vo);
		    	   if(null != p){
		    		   workflowService.claimTask(vo);
		    	   }
		    	   
		    }else{
		    	vo.setUserInfo(userInfo);
		    	vo.setHandleAdviceDes("不同意");
		    	vo.setHandleParam("isNext");
		    	vo.setHandleParamValue(false);
		    	ProcessInstance instance=workflowService.complete(vo);
		    	if(instance==null){
		    		//执行成功
		    		car.setStatus("4");
			    	customerService.updateCustomerService(car);
		    	}
		    }
		}else if("1".equals(status)){//退单
			vo.setUserInfo(userInfo);
			workflowService.backProcessToStart(vo);
			car.setStatus("2");//退单标示为
			customerService.updateCustomerService(car);
		}else if("5".equals(status)){//关闭
			vo.setUserInfo(userInfo);
			workflowService.deleteProcessInstance(vo);
			car.setStatus("5");
			customerService.updateCustomerService(car);
		}
		return Constants.SUCCESS;
	}
	@RequestMapping("/handleServicePage")
	public  ModelAndView  handleServicePage(@RequestParam("pkCareformId")String pkCareformId) throws RDPException{
		UserInfo info=UserUtil.getUserFromSession(); 
		ModelAndView mav= new ModelAndView("file/file_handleService");
		mav.addObject("entity", customerService.findServiceById(pkCareformId,info.getUserId()));
		List<GblSecBranEntity> branList=null;
		GblSecBranEntity bran=new GblSecBranEntity();
		if(Constants.HEAD_OFFICE_CODE.equals(info.getAbbrCode())){
			bran.setPkBranId(info.getBranId());
			branList=commonService.getBranList(bran);
		}else{
			ArrayList<String> list=new ArrayList<String>();
			list.add(Constants.HEAD_BRAN_ID);
			list.add(info.getBranId());
			bran.setIncludeId(list);
			branList=commonService.getBranList(bran);
		}
		mav.addObject("branList", branList);
		mav.addObject("branId", info.getBranId());
		return mav;
	}
	/**
	 * 获取制定门店的用户信息
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("/selectUsesJobInfo")
	@ResponseBody
	public List<SecUsrEntity> selectUsesJobInfo(@RequestParam("fkBranId")String fkBranId) throws RDPException{
		UserInfo info=UserUtil.getUserFromSession(); 
		SecUsrEntity entity=new SecUsrEntity();
		entity.setFkBranId(fkBranId);
		entity.setPkUsrId(info.getUserId());
		return userService.selectUsesJobInfo(entity);
	}
	/**
	 * 查看流程的处理信息
	 * @param pkCareformId
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@RequestMapping("/readInfo")
	public ModelAndView readInfo(@RequestParam("pkCareformId")String pkCareformId) throws RDPException{
		UserInfo info=UserUtil.getUserFromSession(); 
		ModelAndView mav= new ModelAndView("file/file_readServiceInfo");
		CarCareFormEntity entity=customerService.findServiceById(pkCareformId,info.getUserId());
		mav.addObject("entity", entity);
		return mav;
	}
	/**
	 *重新申请
	 * @param pkCareformId
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@RequestMapping("/reApplyService")
	public ModelAndView reApplyService(@RequestParam("pkCareformId")String pkCareformId) throws RDPException{
		ModelAndView mav= new ModelAndView("file/file_reApplyService");
		UserInfo user=UserUtil.getUserFromSession(); 
		List<GblSecBranEntity> branList=null;
		GblSecBranEntity bran=new GblSecBranEntity();
		if(Constants.HEAD_OFFICE_CODE.equals(user.getAbbrCode())){
			bran.setPkBranId(user.getBranId());
			branList=commonService.getBranList(bran);
		}else{
			ArrayList<String> list=new ArrayList<String>();
			list.add(Constants.HEAD_BRAN_ID);
			list.add(user.getBranId());
			bran.setIncludeId(list);
			branList=commonService.getBranList(bran);
		}
		mav.addObject("branList", branList);
		mav.addObject("branId", user.getBranId());
		mav.addObject("baseEntity",customerService.findServiceById(pkCareformId, user.getUserId()));
		mav.addObject("handEntity", customerService.findForApply(pkCareformId));
		return mav;
	}
	/**
	 * 重新提交申请操作
	 * @param entity
	 * @param session
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月25日
	 */
	@RequestMapping("/reHandleApply")
	@ResponseBody
	public  String reHandleApply(CarCareFormEntity entity) throws RDPException{
		UserInfo user=UserUtil.getUserFromSession(); 
		WorkflowParamVo workflowparam=new WorkflowParamVo();
		workflowparam.setProcessInstanceId(entity.getFkFlowinstId());
		workflowparam.setHandleAdviceDes(ConstantUtil.WORKFLOW_STATUS_REAPPLY);
		workflowparam.setUserInfo(UserUtil.getUserFromSession());
		ProcessInstance instance=workflowService.complete(workflowparam);
		if(instance!=null){
			customerService.updateCustomerService(entity);
			 if(StringUtil.isNotBlank(user.getBranId())){
					//如果是门店的话就进行数据上传
					if(user.getBranId().indexOf("ZB") == -1){
						CarCareFormEntity car=  customerService.findServiceById(entity.getPkCareformId(), "");
						if("".equals(car.getFkMemId())){
								car.setFkMemId(null);
						}
						das.executeServer(car, "update");
					}
				}
			 WorkflowParamVo vo=new WorkflowParamVo();
			 vo.setProcessInstanceId(entity.getFkFlowinstId());
			 vo.setClaimUserId(entity.getHandleId());//总监
			  workflowService.claimTask(vo);
		}
		return Constants.SUCCESS;
	}

		
}
